from apps.API.electricSigature.interfacePart11Sys import system
from apps.API.electricSigature.models import ElectronicRecord,AuditUsers
from apps.API.connector.db_common import dic_query_info
from apps import db
from functools import wraps
class Part11AuditRecord:
    @classmethod
    def audit_add_user(cls,user_id, name, role, password):
        try:
            system.add_user(
                user_id=user_id,
                name=name,
                role=role,
                password=password
            )
            system.generate_key_pair(user_id)
            print("Generated key pair for admin")
        except Exception as e:
            print(str(e))
    @classmethod
    def create_sign_record(cls,record_id,record_type,content,created_by,password,passd_check=True):
        try:
            results=ElectronicRecord.query.filter(ElectronicRecord.record_id==record_id).first()
            if results == None:
                record=system.create_signed_record(
                    record_id=record_id,
                    record_type=record_type,
                    content=content,
                    created_by=created_by,
                    password=password,
                    passd_check=passd_check
                )
                print(f"Created and signed record: {record.record_id}")
            elif results != None:
                print(f"Fail to create the duplicate record: {record_id}")
        except Exception as e:
            print(str(e))

    @classmethod
    def verify_record_signatures(cls,record_id):
        try:
            is_valid,results=system.verify_record_signatures(record_id=record_id)
            return is_valid,results
        except Exception as e:
            print(str(e))
    @classmethod  
    def _create_record_id(cls):
        results=ElectronicRecord.query.with_entities(ElectronicRecord.record_id).all()
        record_id = ""
        if results == None:
            record_id = "DOC-1"
        elif results != None:
            number=max([int(result[0].split("-")[1]) for result in results if result[0] !=""])
            number=int(number) + 1
            record_id=f'DOC-{number}'
        return record_id
    @classmethod
    def add_aduit_record(cls,record_type,username,content):
        try:
            if record_type in ["Login_Success"]:
                pass
            else:
                user=AuditUsers.query.filter(AuditUsers.name==username).first()
                if user:
                    created_by=user.user_id
                    record_id=cls._create_record_id()
                    cls.create_sign_record(
                        record_id=record_id,
                        record_type=record_type,
                        content=content,
                        created_by=created_by,
                        password="",
                        passd_check=False
                    )
                else:
                    print(user.name + '不是审计追踪对象')
        except Exception as e:
            print(str(e))
    @classmethod
    def add_record_signature(self,record_id,user_id,signing_purpose,password):
        try:
            signature = system.add_record_signature(
                record_id=record_id,
                user_id=user_id,
                signing_purpose=signing_purpose,
                password=password,
            )
            print(f"Added approval signature by {signature.user_id}")
        except Exception as e:
            print(str(e))
    @classmethod
    def audit_record(self,content): 
        def outer(func):
            @wraps(func)
            def wrapper(*args,**kwargs):
                print(content)
                func(*args,**kwargs)
            return wrapper
        return outer